package com.source.thread.cc;

import java.util.concurrent.atomic.AtomicInteger;

// volatile 不保证原子性

import java.util.concurrent.CountDownLatch;

// 计数器
public class CountDownLatchDemo {
    public static void main(String[] args) throws InterruptedException {
        // 总数是6，必须要执行任务的时候，再使用！


        for (int i = 0; i < 2; i++) {
            final  int temp =i;
            new Thread(()->{
                System.out.println("hahahrrrrrrrrrrrr"+temp);
            }).start();
        }
        CountDownLatch countDownLatch = new CountDownLatch(6);

        for (int i = 1; i <=8 ; i++) {
            new Thread(()->{
                System.out.println(Thread.currentThread().getName()+" Go out");
                countDownLatch.countDown(); // 数量-1
            },String.valueOf(i)).start();
            if(i==6){
                countDownLatch.await();
            }
        }

    //   countDownLatch.await(); // 等待计数器归零，然后再向下执行

        System.out.println("Close Door");

        for (int i = 0; i < 2; i++) {
            final  int temp =i;
            new Thread(()->{
                System.out.println("hahah"+temp);
            }).start();
        }

    }
}